using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Web.Security;
using System.Data.Entity;
using DomainModel.Concrete;
using DomainModel.Entities;

namespace WebUI.Controllers
{

    public class CompanyTypeController : Controller
    {
        SqlCompanyTypeRepository _companyTypeRepository;
        #region PrivateFunctions
        private ActionResult HandleMyError(Exception ex, string actionName)
        {
            var modelErr = new HandleErrorInfo(ex, "CompanyTypeController", actionName);
            return View("Error", modelErr);
        }
        #endregion
        public CompanyTypeController() { _companyTypeRepository = new SqlCompanyTypeRepository(); }
        #region PublicActions
        public ActionResult Index()
        {
            try
            {
                return View(_companyTypeRepository.GetAll());
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "CreateAction");
            }

        }

        public ActionResult Details(int id)
        {
            try
            {
                return View(_companyTypeRepository.GetById(id));
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "CreateAction");
            }
        }

        public ActionResult Create()
        {
            try
            {
                CompanyType companyType = new CompanyType();
                return View(companyType);
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "CreateAction");
            }

        }

        [HttpPost]
        public ActionResult Create(CompanyType companytype, FormCollection collection)
        {
            companytype.LastUpdate = DateTime.Now;
            companytype.UserName = Membership.GetUser().UserName;

            if (ModelState.IsValid)
            {
                try
                {        

                    _companyTypeRepository.Insert(companytype);
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    return HandleMyError(ex, "CreateAction");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult Edit(int id)
        {
            return View(_companyTypeRepository.GetById(id));
        }

        [HttpPost]
        public ActionResult Edit(CompanyType companytype, int id, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                try
                {

                    _companyTypeRepository.Update(companytype);
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    return HandleMyError(ex, "EditAction");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult Delete(int id)
        {
            return View(_companyTypeRepository.GetById(id));
        }

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                _companyTypeRepository.Delete(_companyTypeRepository.GetById(id));
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "DeleteAction");
            }
        }
        #endregion
    }
}
